/*
package com.yonyou.hxjy.service.bm;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import com.yonyou.hxjy.controller.SM2Example;
import com.yonyou.hxjy.utils.BuildToken;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class TaskService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskService.class);
    @Autowired
    public BmYonsuiteService bmYonsuiteService;
    @Value("${bodyEncryptionKey}")
    private String bodyEncryptionKey;
    @Value("${signEncryptionPrivateKey}")
    private String signEncryptionPrivateKey;
    @Value("${bodyDecryptionKey}")
    private String bodyDecryptionKey;
    @Value("${TARGET_URL}")
    private String TARGET_URL;
    @Value("${APP_ID}")
    private String APP_ID;
    @Value("${app_secret}")
    private String app_secret;
    @Value("${grant_type}")
    private String grant_type;

    @Scheduled(fixedRate = 600000)
    public void task1() {
            int pageIndex=1;
            int maxRetries = 3;
            List<Integer> statementdetailstatus=  new ArrayList<>();
            statementdetailstatus.add(2);
            for (int i = 0; i < maxRetries; i++) {
                try {
                    HashMap<String,Object> params=new HashMap<>();
                    params.put("pageIndex", pageIndex);
                    params.put("pageSize", 100);
                    params.put("receipttype", 2);
                    params.put("settlemode_code", "system_0001");
                    params.put("statementdetailstatus", statementdetailstatus);
                    params.put("isSum", false);
                    // 业务逻辑
                    JSONObject settlebench = bmYonsuiteService.settlebench(params);
                    JSONArray jsonArray = settlebench.getJSONObject("data").getJSONArray("recordList");
                    if(jsonArray.size()==0){
                        break; // 没有数据则退出循环
                    }
                    if (!"200".equals(settlebench.getStr("code"))){
                        String errorMsg = "结算工作台列表查询失败: " + settlebench.getStr("message");
                        LOGGER.error(errorMsg);
                        throw new Exception(errorMsg);
                    }
                    Integer endPageIndex=settlebench.getJSONObject("data").getInt("endPageIndex");
                    if (endPageIndex>1){
                        for (int s = 2; s <= endPageIndex; s++) {
                            params.put("pageIndex", pageIndex);
                            params.put("pageSize", 100);
                            params.put("receipttype", 2);
                            params.put("settlemode_code", "system_0001");
                            params.put("statementdetailstatus", statementdetailstatus);
                            params.put("isSum", false);
                            JSONObject settlebenchxh = bmYonsuiteService.settlebench(params);
                            jsonArray.add( settlebenchxh.getJSONObject("data").getJSONArray("recordList"));
                            // 这里写你的循环体代码
                        }
                    }
                    for (int j = 0; j < jsonArray.size(); j++) {
                        // 获取当前索引处的JSONObject
                        JSONObject jsonObject = jsonArray.getJSONObject(j);
                        // 获取detailid   明细行id
                        String detailid = jsonObject.getStr("detailid");
                        JSONObject dataJson = payDetail(detailid);
                        if(!"0".equals(dataJson.getStr("code"))){
                            throw new Exception(dataJson.getStr("msg"));
                        }
                        JSONArray dataArray  = dataJson.getJSONArray("data");
                        //当status=4的时候，取payStatus=jhg的值，除了成功，其余判定为支付失败
                        String errorMsg = dataArray.getJSONObject(0).getStr("errorMsg");
                        String payStatus = dataArray.getJSONObject(0).getStr("payStatus");
                        String status = dataArray.getJSONObject(0).getStr("status");

                        HashMap<String,Object> settleFeedbackMap=new HashMap<>();
                        settleFeedbackMap.put("settleDetailAId", detailid);

                        if ("4".equals(status)) {

                            settleFeedbackMap.put("statementdetailstatus", "g".equals(payStatus) ? 3 : 4);


                            if (!"g".equals(payStatus)) {
                                settleFeedbackMap.put("settlefailreason", errorMsg);
                            }

                            JSONObject settleFeedback = bmYonsuiteService.settleFeedback(settleFeedbackMap);
                            if (!"200".equals(settleFeedback.getStr("code"))) {
                                String errorMsg1 = "结算工作台列表查询失败: " + settlebench.getStr("message");
                                LOGGER.error(errorMsg1);
                                throw new Exception(errorMsg1);
                            }
                        }

                    }

                    break; // 成功则退出循环
                } catch (Exception e) {
                    if (i == maxRetries - 1) {
                        LOGGER.error("任务重试{}次后仍失败: {}", maxRetries, e.getMessage());
                    }
                }
            }

    }

    public JSONObject payDetail(String referenceNum) throws Exception {
        cn.hutool.json.JSONObject datajson =new cn.hutool.json.JSONObject();
        if (APP_ID == null || app_secret == null || grant_type == null) {
            throw new Exception("配置参数（APP_ID/app_secret/grant_type）不能为 null");
        }
        BuildToken selfBuildToken = new BuildToken();
        String token = selfBuildToken.getToken(APP_ID, app_secret, grant_type, TARGET_URL);
        if (token == null) {
            datajson.set("code","999");
            datajson.set("msg","toekn 生成失败为空");
            return datajson;
        }
        String uri = TARGET_URL + "/openapi/payment/openapi/v2/detail";
        CloseableHttpClient client = HttpClients.custom()
                // 禁止HttpClient自动解压缩
                .disableContentCompression()
                .build();


        JSONObject object=new JSONObject();
        object.set("referenceNum",referenceNum);

        HttpPost httpPost = SM2Example.setupRequest(JSONUtil.toJsonStr(object), signEncryptionPrivateKey, uri, bodyEncryptionKey, token);
        //System.out.println("httpPost"+httpPost);
        try (CloseableHttpResponse response = client.execute(httpPost)) {
            byte[] finalResponseData = SM2Example.handleResponse(response,bodyDecryptionKey);
            LOGGER.info("\n返回结果：{}", new String(finalResponseData));
            datajson= JSONUtil.parseObj(new String(finalResponseData));
            return datajson;
        }catch (Exception ignored){
            throw new Exception("网络连接失败或超时！");
        }finally {
            client.close();
        }

    }



}
*/
